/*
 * Copyright 2007 Peter Zelenay.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *     
 */
package net.zelenay.jcrviewer.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.concurrent.ExecutionException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

import net.zelenay.jcrviewer.control.RepositoryController;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 
 * @author Peter Zelenay
 * 
 */
public class RepositorySelector extends JFrame implements
		ApplicationContextAware, InitializingBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ApplicationContext ctx;

	private JButton quickConnect;

	private JButton localConnect;

	private RepositoryController defaultController;

	private RepositoryController localController;
	
	private RepositoryTabs repoTabs;

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		ctx = applicationContext;
	}

	public void afterPropertiesSet() throws Exception {
		setLayout(new GridBagLayout());
		GridBagConstraints gbc;
		
		JPanel toolbar = initToolbar();
		gbc = new GridBagConstraints();
		gbc.anchor = GridBagConstraints.LAST_LINE_START;
		gbc.fill = GridBagConstraints.NONE;
		gbc.gridx = 0;
		gbc.gridy = 0;
		getContentPane().add(toolbar, gbc);
		
		gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 1;
		gbc.weighty = 1;
		getContentPane().add(getRepoTabs(), gbc);
	}

	/**
	 * @return the defaultController
	 */
	public RepositoryController getDefaultController() {
		return defaultController;
	}

	/**
	 * @param defaultController
	 *            the defaultController to set
	 */
	public void setDefaultController(RepositoryController defaultController) {
		this.defaultController = defaultController;
	}

	public RepositoryController getLocalController() {
		return localController;
	}

	public void setLocalController(RepositoryController localController) {
		this.localController = localController;
	}

	public RepositoryTabs getRepoTabs() {
		return repoTabs;
	}

	public void setRepoTabs(RepositoryTabs repoTabs) {
		this.repoTabs = repoTabs;
	}
	
	private JPanel initToolbar() {
		JPanel toolbar = new JPanel();
		toolbar.setLayout(new BoxLayout(toolbar,BoxLayout.LINE_AXIS));
		
		quickConnect = new JButton(getMessage("selector.quickConnect"));
		quickConnect.addActionListener(new OpenRepositoryAction(getDefaultController()));
		toolbar.add(quickConnect);
		
		localConnect = new JButton(getMessage("selector.localRepository"));
		localConnect.addActionListener(new OpenRepositoryAction(getLocalController()));
		toolbar.add(localConnect);
		
		return toolbar;
	}

	private String getMessage(String code) {
		String tmp = ctx.getMessage(code, null, Locale.getDefault());
		return tmp;
	}

	class OpenRepositoryAction implements ActionListener {

		private RepositoryController controller;

		private OpenRepositoryAction(RepositoryController aController) {
			controller = aController;
		}

		public void actionPerformed(final ActionEvent e) {
			((JButton) e.getSource()).setEnabled(false);
			new SwingWorker<ViewerPanel, Object>() {
				@Override
				protected ViewerPanel doInBackground() throws Exception {
					ViewerPanel fr = controller.getViewPanel();
					return fr;
				}

				@Override
				protected void done() {
					try {
						ViewerPanel newView = get();
						getRepoTabs().addRepository(newView.getName(), newView);
						((JButton) e.getSource()).setEnabled(true);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ExecutionException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}.execute();
		}
	}
}
